package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.*;
import org.jiuling.aliyun.sms.dto.SignFileListDto;
import org.jiuling.aliyun.sms.enums.SignSourceEnum;
import org.jiuling.aliyun.sms.enums.SignTypeEnum;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsAsynchronous;
import org.jiuling.aliyun.sms.factory.service.AliYunSignatureManagementService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunSignatureManagementVerify;
import org.jiuling.aliyun.sms.model.*;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * 阿里云异步签名管理Service实现类
 * @author 紫灵
 * @date 2023年02月16日 20时53分15秒
 */
public class AliYunAsyncSignatureManagementServiceImpl extends AliYunSmsAsynchronous implements AliYunSignatureManagementService {

    public AliYunAsyncSignatureManagementServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddSmsSignResponseModel addSmsSign(SignSourceEnum signSource, String remark, List<SignFileListDto> signFileList, SignTypeEnum signType,String signName) {
        // 校验添加短信签名
        AliYunSignatureManagementVerify.addSmsSignParam(signSource, remark, signFileList, signName);
        AddSmsSignRequest addSmsSignRequest = AddSmsSignRequest.builder()
                .signName(signName)
                .signSource(signSource.getCode())
                .remark(remark)
                // 构建签名文件列表
                .signFileList(signFileList.stream().map(signFile ->AddSmsSignRequest.SignFileList.builder()
                            .fileContents(signFile.getFileContents())
                            .fileSuffix(signFile.getFileSuffix().name())
                            .build()).collect(Collectors.toList()))
                .signType(signType.getCode())
                .build();
        try (AsyncClient asyncClient = create()){
            return AddSmsSignResponseConvert.bodyToModel(asyncClient.addSmsSign(addSmsSignRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteSmsSignResponseModel deleteSmsSign(String signName) {
        AliYunSignatureManagementVerify.deleteSmsSignParam(signName);
        DeleteSmsSignRequest deleteSmsSignRequest = DeleteSmsSignRequest.builder()
                .signName(signName)
                .build();
        try (AsyncClient asyncClient = create()){
            return DeleteSmsSignResponseConvert.bodyToModel(asyncClient.deleteSmsSign(deleteSmsSignRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public ModifySmsSignResponseModel modifySmsSign(String signName, SignSourceEnum signSource, String remark, List<SignFileListDto> signFileList, SignTypeEnum signType) {
        AliYunSignatureManagementVerify.modifySmsSignParam(signSource, remark, signFileList, signName);
        ModifySmsSignRequest modifySmsSignRequest = ModifySmsSignRequest.builder()
                .signName(signName)
                .signSource(signSource.getCode())
                .remark(remark)
                .signType(signType.getCode())
                // 构建签名文件列表
                .signFileList(signFileList.stream().map(signFile ->ModifySmsSignRequest.SignFileList.builder()
                        .fileContents(signFile.getFileContents())
                        .fileSuffix(signFile.getFileSuffix().name())
                        .build()).collect(Collectors.toList()))
                .build();
        try (AsyncClient asyncClient = create()){
           return  ModifySmsSignResponseConvert.bodyToModel(asyncClient.modifySmsSign(modifySmsSignRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QuerySmsSignListResponseModel querySmsSignList(int pageNum, int pageSize) {
        QuerySmsSignListRequest querySmsSignListRequest = QuerySmsSignListRequest.builder()
                .pageIndex(pageNum)
                .pageSize(pageSize)
                .build();
        try (AsyncClient asyncClient = create()){
            return QuerySmsSignListResponseConvert.bodyToModel(asyncClient.querySmsSignList(querySmsSignListRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }

    }

    @Override
    public QuerySmsSignResponseModel querySmsSign(String signName) {
        AliYunSignatureManagementVerify.querySmsSignParam(signName);
        QuerySmsSignRequest querySmsSignRequest = QuerySmsSignRequest.builder()
                .signName(signName)
                .build();
        try (AsyncClient asyncClient = create()){
            return QuerySmsSignResponseConvert.bodyToModel(asyncClient.querySmsSign(querySmsSignRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
